package com.example.myanimator.scroll;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myanimator.R;
import com.example.myanimator.databinding.ActivityScrollerItemBinding;

import java.util.List;

/**
 * @author tgw
 * @date 2021/11/22
 * @describe
 */


class RecycleViewImageAdapter extends RecyclerView.Adapter<RecycleViewImageAdapter.ImageViewHolder> {


    private List<String> imageViewList;
    private Context mContext;

    public RecycleViewImageAdapter(List<String> imageViewList, Context mContext) {
        this.imageViewList = imageViewList;
        this.mContext = mContext;
    }


    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ActivityScrollerItemBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(mContext),
                R.layout.activity_scroller_item,
                parent,
                false
        );
        return new ImageViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        Glide.with(mContext).load(imageViewList.get(position % imageViewList.size())).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
        }
    }
}

